import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

import 'binding.dart';
import 'ecgcontroller.dart';
import 'ecgwave.dart';
import 'setting.dart';

Map<String, String> queryHashUriParameters(String fragment) {
  var ret = <String, String>{};
  var f1 = fragment.split('?');
  if (f1.isNotEmpty && f1.length == 2) {
    var f2 = f1[1].split('&');
    if (f2.isNotEmpty) {
      for (var i = 0; i < f2.length; i++) {
        var f3 = f2[i].split('=');
        if (f3.isNotEmpty && f3.length == 2) {
          ret[f3[0]] = f3[1];
        }
      }
    }
  }
  return ret;
}

void main() async {
  await GetStorage.init();
  //http://localhost:55323/#/home?para1=abc
  String address = Uri.base.toString();
  var params = queryHashUriParameters(Uri.base.fragment);
  print('params: $params');
  String ecguid = params['ecguid'] ?? '1000';
  print('ecguid:$ecguid');

  runApp(GetMaterialApp(
    debugShowCheckedModeBanner: false,
    initialRoute: '/home',
    getPages: [
      GetPage(
          name: '/home',
          page: () => FlutterSmartDialog(child: Home()),
          // binding: EcgBinding(sn: "fxrmyyecg001", rd: "20210623150200")),
          // binding: EcgBinding(sn: "dlfyecg001", rd: "20230316")),
          // 1003 - xn0b22
          // 1004 - fxj97k8
          binding: EcgBinding(sn: ecguid, rd: "")),
    ],
  ));
}

class Home extends GetView<EcgController> {
  Widget ecgWaveIBuild() {
    return Row(children: [
      Obx(() => Container(
            width: 75,
            height: 88,
            padding: const EdgeInsets.all(5),
            decoration: const BoxDecoration(
              color: Colors.black,
              border:
                  Border(bottom: BorderSide(width: 1, color: Colors.white60)),
            ),
            child: Stack(
              children: [
                const Positioned(
                  top: 0,
                  left: 0,
                  child: Text('I',
                      style: TextStyle(fontSize: 16, color: Colors.white)),
                ),
                Positioned(
                  top: 20,
                  left: 0,
                  child: Text(controller.ecgWaveIContext.value.yZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
                Positioned(
                  top: 36,
                  left: 0,
                  child: Text(controller.ecgWaveIContext.value.xZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
              ],
            ),
          )),
      Obx(() => EcgWaveChart(
            waveContext: controller.ecgWaveIContext.value,
            points: controller.waveI.value,
            paused: controller.wavePaused.value,
          )),
      Container(
        width: 10,
        height: 88,
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(bottom: BorderSide(width: 1, color: Colors.white60)),
        ),
      )
    ]);
  }

  Widget ecgWaveIIBuild() {
    return Row(children: [
      Obx(() => Container(
            width: 75,
            height: 88,
            padding: const EdgeInsets.all(5),
            decoration: const BoxDecoration(
              color: Colors.black,
              border:
                  Border(bottom: BorderSide(width: 1, color: Colors.white60)),
            ),
            child: Stack(
              children: [
                const Positioned(
                  top: 0,
                  left: 0,
                  child: Text('II',
                      style: TextStyle(fontSize: 16, color: Colors.white)),
                ),
                Positioned(
                  top: 20,
                  left: 0,
                  child: Text(controller.ecgWaveIIContext.value.yZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
                Positioned(
                  top: 36,
                  left: 0,
                  child: Text(controller.ecgWaveIIContext.value.xZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
              ],
            ),
          )),
      Obx(() => EcgWaveChart(
            waveContext: controller.ecgWaveIIContext.value,
            points: controller.waveII.value,
            paused: controller.wavePaused.value,
          )),
      Container(
        width: 10,
        height: 88,
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(bottom: BorderSide(width: 1, color: Colors.white60)),
        ),
      )
    ]);
  }

  Widget ecgWaveIIIBuild() {
    return Row(children: [
      Obx(() => Container(
            width: 75,
            height: 88,
            padding: const EdgeInsets.all(5),
            decoration: const BoxDecoration(
              color: Colors.black,
              border:
                  Border(bottom: BorderSide(width: 1, color: Colors.white60)),
            ),
            child: Stack(
              children: [
                const Positioned(
                  top: 0,
                  left: 0,
                  child: Text('III',
                      style: TextStyle(fontSize: 16, color: Colors.white)),
                ),
                Positioned(
                  top: 20,
                  left: 0,
                  child: Text(controller.ecgWaveIIIContext.value.yZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
                Positioned(
                  top: 36,
                  left: 0,
                  child: Text(controller.ecgWaveIIIContext.value.xZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
              ],
            ),
          )),
      Obx(() => EcgWaveChart(
            waveContext: controller.ecgWaveIIIContext.value,
            points: controller.waveIII.value,
            paused: controller.wavePaused.value,
          )),
      Container(
        width: 10,
        height: 88,
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(bottom: BorderSide(width: 1, color: Colors.white60)),
        ),
      )
    ]);
  }

  Widget ecgWaveAVLBuild() {
    return Row(children: [
      Obx(() => Container(
            width: 75,
            height: 88,
            padding: const EdgeInsets.all(5),
            decoration: const BoxDecoration(
              color: Colors.black,
              border:
                  Border(bottom: BorderSide(width: 1, color: Colors.white60)),
            ),
            child: Stack(
              children: [
                const Positioned(
                  top: 0,
                  left: 0,
                  child: Text('AVL',
                      style: TextStyle(fontSize: 16, color: Colors.white)),
                ),
                Positioned(
                  top: 20,
                  left: 0,
                  child: Text(controller.ecgWaveAVLContext.value.yZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
                Positioned(
                  top: 36,
                  left: 0,
                  child: Text(controller.ecgWaveAVLContext.value.xZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
              ],
            ),
          )),
      Obx(() => EcgWaveChart(
            waveContext: controller.ecgWaveAVLContext.value,
            points: controller.waveAVL.value,
            paused: controller.wavePaused.value,
          )),
      Container(
        width: 10,
        height: 88,
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(bottom: BorderSide(width: 1, color: Colors.white60)),
        ),
      )
    ]);
  }

  Widget ecgWaveAVRBuild() {
    return Row(children: [
      Obx(() => Container(
            width: 75,
            height: 88,
            padding: const EdgeInsets.all(5),
            decoration: const BoxDecoration(
              color: Colors.black,
              border:
                  Border(bottom: BorderSide(width: 1, color: Colors.white60)),
            ),
            child: Stack(
              children: [
                const Positioned(
                  top: 0,
                  left: 0,
                  child: Text('AVR',
                      style: TextStyle(fontSize: 16, color: Colors.white)),
                ),
                Positioned(
                  top: 20,
                  left: 0,
                  child: Text(controller.ecgWaveAVRContext.value.yZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
                Positioned(
                  top: 36,
                  left: 0,
                  child: Text(controller.ecgWaveAVRContext.value.xZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
              ],
            ),
          )),
      Obx(() => EcgWaveChart(
            waveContext: controller.ecgWaveAVRContext.value,
            points: controller.waveAVR.value,
            paused: controller.wavePaused.value,
          )),
      Container(
        width: 10,
        height: 88,
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(bottom: BorderSide(width: 1, color: Colors.white60)),
        ),
      )
    ]);
  }

  Widget ecgWaveAVFBuild() {
    return Row(children: [
      Obx(() => Container(
            width: 75,
            height: 88,
            padding: const EdgeInsets.all(5),
            decoration: const BoxDecoration(
              color: Colors.black,
              border:
                  Border(bottom: BorderSide(width: 1, color: Colors.white60)),
            ),
            child: Stack(
              children: [
                const Positioned(
                  top: 0,
                  left: 0,
                  child: Text('AVF',
                      style: TextStyle(fontSize: 16, color: Colors.white)),
                ),
                Positioned(
                  top: 20,
                  left: 0,
                  child: Text(controller.ecgWaveAVFContext.value.yZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
                Positioned(
                  top: 36,
                  left: 0,
                  child: Text(controller.ecgWaveAVFContext.value.xZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
              ],
            ),
          )),
      Obx(() => EcgWaveChart(
            waveContext: controller.ecgWaveAVFContext.value,
            points: controller.waveAVF.value,
            paused: controller.wavePaused.value,
          )),
      Container(
        width: 10,
        height: 88,
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(bottom: BorderSide(width: 1, color: Colors.white60)),
        ),
      )
    ]);
  }

  Widget ecgWaveVBuild() {
    return Row(children: [
      Obx(() => Container(
            width: 75,
            height: 88,
            padding: const EdgeInsets.all(5),
            decoration: const BoxDecoration(
              color: Colors.black,
              border:
                  Border(bottom: BorderSide(width: 1, color: Colors.white60)),
            ),
            child: Stack(
              children: [
                const Positioned(
                  top: 0,
                  left: 0,
                  child: Text('V1',
                      style: TextStyle(fontSize: 16, color: Colors.white)),
                ),
                Positioned(
                  top: 20,
                  left: 0,
                  child: Text(controller.ecgWaveV1Context.value.yZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
                Positioned(
                  top: 36,
                  left: 0,
                  child: Text(controller.ecgWaveV1Context.value.xZoom,
                      style:
                          const TextStyle(fontSize: 10, color: Colors.white60)),
                ),
              ],
            ),
          )),
      Obx(() => EcgWaveChart(
            waveContext: controller.ecgWaveV1Context.value,
            points: controller.waveV1.value,
            paused: controller.wavePaused.value,
          )),
      Container(
        width: 10,
        height: 88,
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(bottom: BorderSide(width: 1, color: Colors.white60)),
        ),
      )
    ]);
  }

  Widget respWaveBuild() {
    return Row(children: [
      Container(
        width: 75,
        height: 88,
        padding: const EdgeInsets.all(5),
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(bottom: BorderSide(width: 1, color: Colors.white60)),
        ),
        child: Stack(
          children: const [
            Positioned(
              top: 0,
              left: 0,
              child: Text('Resp',
                  style: TextStyle(fontSize: 16, color: Colors.white)),
            ),
          ],
        ),
      ),
      Obx(() => EcgWaveChart(
            waveContext: controller.respWaveContext.value,
            points: controller.waveResp.value,
            paused: controller.wavePaused.value,
          )),
      Container(
        width: 10,
        height: 88,
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(bottom: BorderSide(width: 1, color: Colors.white60)),
        ),
      )
    ]);
  }

  Widget plethWaveBuild() {
    return Row(children: [
      Container(
        width: 75,
        height: 88,
        padding: const EdgeInsets.all(5),
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(bottom: BorderSide(width: 1, color: Colors.white60)),
        ),
        child: Stack(
          children: const [
            Positioned(
              top: 0,
              left: 0,
              child: Text('Pleth',
                  style: TextStyle(fontSize: 16, color: Colors.white)),
            ),
          ],
        ),
      ),
      Obx(() => EcgWaveChart(
            waveContext: controller.plethWaveContext.value,
            points: controller.waveSpo2.value,
            paused: controller.wavePaused.value,
          )),
      Container(
        width: 10,
        height: 88,
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(bottom: BorderSide(width: 1, color: Colors.white60)),
        ),
      )
    ]);
  }

  Widget hrInfoBuild() {
    return Obx(() => Container(
          width: 248,
          height: 88,
          decoration: const BoxDecoration(
            color: Colors.black,
            border: Border(
              bottom: BorderSide(width: 1, color: Colors.white60),
              left: BorderSide(width: 1, color: Colors.white60),
            ),
          ),
          child: Stack(
            children: [
              const Positioned(
                  top: 5,
                  left: 5,
                  child: Text('HR',
                      style:
                          TextStyle(fontSize: 16, color: Colors.lightGreen))),
              Positioned(
                top: 0,
                left: 0,
                child: SizedBox(
                  width: 248,
                  height: 88,
                  child: Center(
                    child: Text(
                      controller.hr.value < 0
                          ? '--'
                          : controller.hr.value.toString(),
                      style: const TextStyle(
                        fontSize: 48,
                        color: Colors.lightGreen,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ));
  }

  Widget co2InfoBuild() {
    return Container(
      width: 248,
      height: 88,
      decoration: const BoxDecoration(
        color: Colors.black,
        border: Border(
          bottom: BorderSide(width: 1, color: Colors.white60),
          left: BorderSide(width: 1, color: Colors.white60),
        ),
      ),
      child: Stack(
        children: [
          const Positioned(
            top: 5,
            left: 5,
            child: Text(
              'CO2',
              style: TextStyle(
                fontSize: 16,
                color: Colors.orange,
              ),
            ),
          ),
          Positioned(
            top: 0,
            left: 0,
            child: SizedBox(
              width: 248,
              height: 88,
              child: Center(
                child: Text(
                  controller.co2.value.toString(),
                  // '--',
                  style: const TextStyle(
                    fontSize: 48,
                    color: Colors.orange,
                  ),
                ),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget respInfoBuild() {
    return Container(
      width: 248,
      height: 88,
      decoration: const BoxDecoration(
        color: Colors.black,
        border: Border(
          bottom: BorderSide(width: 1, color: Colors.white60),
          left: BorderSide(width: 1, color: Colors.white60),
        ),
      ),
      child: Stack(
        children: [
          const Positioned(
            top: 5,
            left: 5,
            child: Text(
              'RESP',
              style: TextStyle(
                fontSize: 16,
                color: Colors.orange,
              ),
            ),
          ),
          Positioned(
            top: 0,
            left: 0,
            child: SizedBox(
              width: 248,
              height: 88,
              child: Center(
                child: Obx(() => Text(
                      controller.resp.value < 0
                          ? '--'
                          : controller.resp.value.toString(),
                      style: const TextStyle(
                        fontSize: 48,
                        color: Colors.orange,
                      ),
                    )),
              ),
            ),
          ),
        ],
      ),
    );
  }

  Widget spo2InfoBuild() {
    return Obx(() => Container(
          width: 248,
          height: 88,
          decoration: const BoxDecoration(
            color: Colors.black,
            border: Border(
              bottom: BorderSide(width: 1, color: Colors.white60),
              left: BorderSide(width: 1, color: Colors.white60),
            ),
          ),
          child: Stack(
            children: [
              const Positioned(
                top: 5,
                left: 5,
                child: Text(
                  'SPO2',
                  style: TextStyle(
                    fontSize: 16,
                    color: Colors.tealAccent,
                  ),
                ),
              ),
              Positioned(
                top: 0,
                left: 0,
                child: SizedBox(
                  width: 248,
                  height: 88,
                  child: Center(
                    child: Text(
                      controller.spo2Value.value < 0
                          ? '--'
                          : controller.spo2Value.toString(),
                      style: const TextStyle(
                        fontSize: 48,
                        color: Colors.tealAccent,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ));
  }

  Widget nibpInfoBuild() {
    return Obx(() => Container(
          width: 248,
          height: 88,
          decoration: const BoxDecoration(
            color: Colors.black,
            border: Border(
              bottom: BorderSide(width: 1, color: Colors.white60),
              left: BorderSide(width: 1, color: Colors.white60),
            ),
          ),
          child: Stack(
            children: [
              const Positioned(
                top: 5,
                left: 5,
                child: Text(
                  'NIBP',
                  style: TextStyle(
                    fontSize: 16,
                    color: Colors.red,
                  ),
                ),
              ),
              Positioned(
                top: 0,
                left: 0,
                child: SizedBox(
                  width: 248,
                  height: 88,
                  child: Center(
                    child: Text(
                      (controller.nibp_hasresult.value &&
                              controller.nibp_sys.value < 500)
                          ? controller.nibp_sys.value.toString() +
                              '/' +
                              controller.nibp_dia.value.toString()
                          : "--/--",
                      style: const TextStyle(
                        fontSize: 48,
                        color: Colors.red,
                      ),
                    ),
                  ),
                ),
              ),
            ],
          ),
        ));
  }

  Widget tempInfoBuild() {
    return Obx(() => Container(
          width: 248,
          height: 88,
          decoration: const BoxDecoration(
            color: Colors.black,
            border: Border(
              bottom: BorderSide(width: 1, color: Colors.white60),
              left: BorderSide(width: 1, color: Colors.white60),
            ),
          ),
          child: Stack(
            children: [
              const Positioned(
                top: 5,
                left: 5,
                child: Text(
                  'TEMP',
                  style: TextStyle(
                    fontSize: 16,
                    color: Colors.yellow,
                  ),
                ),
              ),
              Positioned(
                top: 0,
                left: 0,
                child: SizedBox(
                  width: 248,
                  height: 88,
                  child: Row(
                    crossAxisAlignment: CrossAxisAlignment.center,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Text(
                        (controller.temp_hasresult.value &&
                                controller.temp.value > 0)
                            ? controller.temp.value.toString()
                            : "--",
                        style: const TextStyle(
                          fontSize: 48,
                          color: Colors.yellow,
                        ),
                      ),
                      const SizedBox(width: 20),
                      Text(
                        (controller.temp2_hasresult.value &&
                                controller.temp2.value > 0)
                            ? controller.temp2.value.toString()
                            : "--",
                        style: const TextStyle(
                          fontSize: 48,
                          color: Colors.yellow,
                        ),
                      )
                    ],
                  ),
                ),
              ),
            ],
          ),
        ));
  }

  Widget pauseButtonBuild() {
    return GestureDetector(
      onTap: () {
        controller.wavePaused.value = !controller.wavePaused.value;
      },
      child: Container(
        width: 248,
        height: 88,
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(
            bottom: BorderSide(width: 1, color: Colors.white60),
            left: BorderSide(width: 1, color: Colors.white60),
          ),
        ),
        child: Center(
          child: Obx(() => Text(
                controller.wavePaused.value ? '恢复' : '暂停',
                style: const TextStyle(
                  fontSize: 32,
                  color: Colors.white,
                ),
              )),
        ),
      ),
    );
  }

  Widget settingButtonBuild() {
    return GestureDetector(
      onTap: () {
        SmartDialog.show(
          alignment: Alignment.centerRight,
          clickMaskDismiss: true,
          builder: (_) => WaveSettingDialog(
            maxWidth: 330,
            controller: controller,
            ecgSelectedXZoom: controller.ecgWaveIContext.value.xZoom,
            ecgSelectedYZoom: controller.ecgWaveIContext.value.yZoom,
            plethSelectedXZoom: controller.plethWaveContext.value.xZoom,
            plethSelectedYZoom: controller.plethWaveContext.value.yZoom,
            respSelectedXZoom: controller.respWaveContext.value.xZoom,
            respSelectedYZoom: controller.respWaveContext.value.yZoom,
          ),
        );
      },
      child: Container(
        width: 248,
        height: 88,
        decoration: const BoxDecoration(
          color: Colors.black,
          border: Border(
            bottom: BorderSide(width: 1, color: Colors.white60),
            left: BorderSide(width: 1, color: Colors.white60),
          ),
        ),
        child: const Center(
          child: Text(
            '设置',
            style: TextStyle(
              fontSize: 32,
              color: Colors.white,
            ),
          ),
        ),
      ),
    );
  }

  @override
  Widget build(context) => Scaffold(
        // appBar: AppBar(title: const Text("心电监护仪数据实时接收")),
        body: Container(
          decoration:
              const BoxDecoration(color: Color.fromARGB(255, 60, 59, 59)),
          child: Center(
            child: FittedBox(
              // width: 627,
              // height: 455,
              // padding: const EdgeInsets.all(5),
              // decoration: BoxDecoration(
              //   color: Colors.black54,
              //   border: Border.all(width: 2, color: Colors.white60),
              // ),
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: <Widget>[
                  // const SizedBox(height: 10),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      ecgWaveIBuild(),
                      hrInfoBuild(),
                    ],
                  ), // I

                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      ecgWaveIIBuild(),
                      // co2InfoBuild(),
                      respInfoBuild(),
                    ],
                  ),

                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      ecgWaveIIIBuild(),
                      spo2InfoBuild(),
                    ],
                  ),

                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      ecgWaveAVRBuild(),
                      nibpInfoBuild(),
                    ],
                  ),

                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      ecgWaveAVLBuild(),
                      tempInfoBuild(),
                    ],
                  ),

                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      ecgWaveAVFBuild(),
                      pauseButtonBuild(),
                    ],
                  ),

                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      ecgWaveVBuild(),
                      Container(
                        width: 248,
                        height: 88,
                        decoration: const BoxDecoration(
                          color: Colors.black,
                          border: Border(
                            bottom: BorderSide(width: 1, color: Colors.white60),
                            left: BorderSide(width: 1, color: Colors.white60),
                          ),
                        ),
                      ),
                    ],
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    crossAxisAlignment: CrossAxisAlignment.center,
                    children: [
                      plethWaveBuild(),
                      settingButtonBuild(),
                    ],
                  ),
                ],
              ),
            ),
          ),
        ),
        // floatingActionButton: FloatingActionButton(
        //   child: Obx(() => Icon(
        //       controller.wavePaused.value ? Icons.play_arrow : Icons.pause)),
        //   onPressed: () {
        //     controller.wavePaused.value = !controller.wavePaused.value;
        //   },
        // ),
      );
}
